﻿using Engine;
using Engine.Graphics;
using Engine.Input;
using Engine.Media;
using GameEntitySystem;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace CommandBlock
{
    /// <summary>
    /// 新的方块面板
    /// </summary>
    public class CommandBlockPanel : CanvasWidget
    {
        public CommandBlockSelectionWidget m_creativeInventoryWidget;

        public ComponentCreativeInventory m_componentCreativeInventory;

        public List<int> m_slotIndices = new List<int>();

        public GridPanelWidget m_inventoryGrid;

        public int m_pagesCount;

        public int m_assignedCategoryIndex = -1;

        public int m_assignedPageIndex = -1;

        public CommandBlockPanel(CommandBlockSelectionWidget creativeInventoryWidget)
        {
            m_creativeInventoryWidget = creativeInventoryWidget;
            m_componentCreativeInventory = creativeInventoryWidget.Entity.FindComponent<ComponentCreativeInventory>(throwOnError: true);
            XElement node = ContentManager.Get<XElement>("Widgets/CreativeInventoryPanel");
            LoadContents(this, node);
            m_inventoryGrid = Children.Find<GridPanelWidget>("InventoryGrid");
            for (int i = 0; i < m_inventoryGrid.RowsCount; i++)
            {
                for (int j = 0; j < m_inventoryGrid.ColumnsCount; j++)
                {
                    XElement node2 = ContentManager.Get<XElement>("Widgets/CommandBlockSlot");
                    ContainerWidget widget = (ContainerWidget)LoadWidget(this, node2, null);
                    BlockIconWidget blockSlot = widget.Children.Find<BlockIconWidget>("BlockSlot.Icon");
                    blockSlot.Value = 0;
                    m_inventoryGrid.Children.Add(widget);
                    m_inventoryGrid.SetWidgetCell(widget, new Point2(j, i));
                }
            }
        }

        public override void Update()
        {
            if (m_assignedCategoryIndex >= 0)
            {
                if (Input.Scroll.HasValue)
                {
                    Widget widget = HitTestGlobal(Input.Scroll.Value.XY);
                    if (widget != null && widget.IsChildWidgetOf(m_inventoryGrid))
                    {
                        m_componentCreativeInventory.PageIndex -= (int)Input.Scroll.Value.Z;
                    }
                }
                if (m_creativeInventoryWidget.PageDownButton.IsClicked)
                {
                    ++m_componentCreativeInventory.PageIndex;
                }
                if (m_creativeInventoryWidget.PageUpButton.IsClicked)
                {
                    --m_componentCreativeInventory.PageIndex;
                }
                m_componentCreativeInventory.PageIndex = ((m_pagesCount > 0) ? MathUtils.Clamp(m_componentCreativeInventory.PageIndex, 0, m_pagesCount - 1) : 0);
            }
            if (m_componentCreativeInventory.CategoryIndex != m_assignedCategoryIndex)
            {
                if (m_creativeInventoryWidget.GetCategoryName(m_componentCreativeInventory.CategoryIndex) == LanguageControl.Get("CreativeInventoryWidget", 2))
                {
                    m_slotIndices = new List<int>(Enumerable.Range(10, m_componentCreativeInventory.OpenSlotsCount - 10));
                }
                else
                {
                    m_slotIndices.Clear();
                    for (int i = m_componentCreativeInventory.OpenSlotsCount; i < m_componentCreativeInventory.SlotsCount; i++)
                    {
                        int slotValue = m_componentCreativeInventory.GetSlotValue(i);
                        int num2 = Terrain.ExtractContents(slotValue);
                        if (BlocksManager.Blocks[num2].GetCategory(slotValue) == m_creativeInventoryWidget.GetCategoryName(m_componentCreativeInventory.CategoryIndex))
                        {
                            m_slotIndices.Add(i);
                        }
                    }
                }
                int num3 = m_inventoryGrid.ColumnsCount * m_inventoryGrid.RowsCount;
                m_pagesCount = (m_slotIndices.Count + num3 - 1) / num3;
                m_assignedCategoryIndex = m_componentCreativeInventory.CategoryIndex;
                m_assignedPageIndex = -1;
                m_componentCreativeInventory.PageIndex = 0;
            }
            if (m_componentCreativeInventory.PageIndex != m_assignedPageIndex)
            {
                int num4 = m_inventoryGrid.ColumnsCount * m_inventoryGrid.RowsCount;
                int num5 = m_componentCreativeInventory.PageIndex * num4;
                foreach (Widget child in m_inventoryGrid.Children)
                {
                    var widget = child as ContainerWidget;
                    BlockIconWidget blockIconWidget = widget.Children.Find<BlockIconWidget>("BlockSlot.Icon");
                    if (widget != null)
                    {
                        if (num5 < m_slotIndices.Count)
                        {
                            blockIconWidget.Value = m_componentCreativeInventory.GetSlotValue(m_slotIndices[num5++]);
                        }
                        else
                        {
                            blockIconWidget.Value = 0;
                        }
                    }
                }
                m_assignedPageIndex = m_componentCreativeInventory.PageIndex;
            }
            foreach (Widget child in m_inventoryGrid.Children)
            {
                var widget = child as ContainerWidget;
                if (widget != null)
                {
                    ButtonWidget buttonWidget = widget.Children.Find<ButtonWidget>("BlockSlot.Button");
                    if (buttonWidget.IsClicked)
                    {
                        BlockIconWidget blockSlot = widget.Children.Find<BlockIconWidget>("BlockSlot.Icon");
                        TextBoxWidget textBoxWidget = (TextBoxWidget)m_creativeInventoryWidget.m_commandEditWidget.m_commandWidgetDatas["TextBox$" + m_creativeInventoryWidget.m_para];
                        BlockIconWidget blockIconWidget = (BlockIconWidget)m_creativeInventoryWidget.m_commandEditWidget.m_commandWidgetDatas["BlockIcon$" + m_creativeInventoryWidget.m_para];
                        textBoxWidget.Text = blockSlot.Value + "";
                        blockIconWidget.Value = blockSlot.Value;
                        m_creativeInventoryWidget.CloseCurrentWidget();
                    }
                }
            }
            if (m_componentCreativeInventory.PageIndex < m_pagesCount - 1)
            {
                m_creativeInventoryWidget.PageDownButton.IsEnabled = true;
                m_creativeInventoryWidget.m_pageDownRectangle.FillColor = new Color(100, 100, 100);
            }
            else
            {
                m_creativeInventoryWidget.PageDownButton.IsEnabled = false;
                m_creativeInventoryWidget.m_pageDownRectangle.FillColor = new Color(80, 80, 80);
            }
            if (m_componentCreativeInventory.PageIndex > 0)
            {
                m_creativeInventoryWidget.PageUpButton.IsEnabled = true;
                m_creativeInventoryWidget.m_pageUpRectangle.FillColor = new Color(100, 100, 100);
            }
            else
            {
                m_creativeInventoryWidget.PageUpButton.IsEnabled = false;
                m_creativeInventoryWidget.m_pageUpRectangle.FillColor = new Color(80, 80, 80);
            }
        }
    }
}